layui.use(['layer', 'form', 'ztree', 'laydate', 'admin', 'ax', 'table', 'func', 'dict', 'formSelects', 'xmSelect','upload'], function () {
    var form = layui.form;
    var $ax = layui.ax;
    var table = layui.table;
    var func = layui.func;
    var upload = layui.upload;
    var $ = jQuery = layui.jquery;
    var dict = layui.dict; //获取自定义模块
    var formSelects = layui.formSelects;
    var xmSelect = layui.xmSelect;
    var laydate = layui.laydate;
    dict.renderDictAll(); //渲染
    //table的初始化实例

    //重新渲染select数据
    form.render('select');

    //日期时间范围
    laydate.render({
        elem: '#costingDateRange',
        range: true,
        format: 'yyyy/MM/dd'
    });

    /**
     * 系统管理--菜单管理
     */
    var Menu = {
        tableId: "menuTable",    //表格id
        condition: {
            menuId: "",
            menuName: "",
            level: ""
        }
    };
    //宽度大小
    var sm = 110;
    var nor = 150;
    var lg = 180;
    var bt = 220;
    /**
     * 初始化表格的列
     */
    Menu.initColumn = function () {
        return [[
            {field: 'costingDate', sort: true, title: '核对日期', templet: function (data) {
                    return data.costingDate.substring(0, 10);
                }},
            {field: 'rawMaterialCost', sort: true, title: '原料成本'},
            {field: 'artificialCost', sort: true, title: '人工成本'},
            {field: 'packageCost', sort: true, title: '包装成本'},
            {field: 'relationshipCost', sort: true, title: '关系维护成本'},
            {field: 'operationCost', sort: true, title: '运营成本'},
            {field: 'orderAllIncome', sort: true, title: '今日订单总收入'},
            {field: 'allProfit', sort: true, title: '今日利润'},
            {field: 'status', sort: true, title: '核算状态', templet: function (data) {
                    var status = data.status;
                    if (status === '0') {
                        return '待核算';
                    }
                    if (status === '1') {
                        return '已核算';
                    }
                    return '待核算';
                }},
            {field: 'createdAt', sort: true, title: '创建时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
        ]];
    };


    Menu.initTable = function(queryData) {
        var path = '/erya/costing/queryAll';
        if (queryData) {
            path += '?' + queryData;
        }
        return table.render({
            elem: '#' + Menu.tableId,
            url: Feng.ctxPath + path,
            page: true,
            height: "full-98",
            cellMinWidth: 100,
            cols: Menu.initColumn(),
        });
    }

    // 搜索
    $('#btnQuery').click(function () {
        var costingDateRange = $('#costingDateRange').val().trim();
        Menu.initTable('costingDateRange=' +　costingDateRange);
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        func.open({
            height: 630,
            title: '添加核算',
            content: Feng.ctxPath + '/erya/costing/costingAdd',
            endCallback: function () {
                table.reload(Menu.tableId);
            }
        });
    });


    Menu.initTable(null);

    Menu.onEditRole = function (data) {
        if (data.status === '1') {
            Feng.error("已核算数据不支持重新核算!");
            return;
        }
        func.open({
            height: 630,
            title: '修改核算',
            content: Feng.ctxPath + "/erya/costing/costingEdit?id=" + data.id,
            tableId: Menu.tableId
        });
    }

    Menu.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/erya/costing/delete/" + data.id, function (data) {
                Feng.success("删除成功!");
                table.reload(Menu.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("formId", data.formId);
            ajax.start();
            table.reload(Menu.tableId);
        };
        Feng.confirm("是否删除该条核算记录！", operation);
    };

    // 工具条点击事件
    table.on('tool(' + Menu.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Menu.onEditRole(data);
        }else if (layEvent === 'delete') {
            Menu.onDeleteItem(data);
        }
    });

    $('#downloadExcel').click(function () {
        var costingDateRange = $('#costingDateRange').val().trim();
        window.location.href=Feng.ctxPath + '/erya/costing/downLoad?dateRange=' + costingDateRange;
    });

    $('#costingHistoryOrder').click(function () {
        var ajax = new $ax(Feng.ctxPath + "/erya/costing/costingHistoryOrder", function (data) {
            Feng.success("核算成功!");
            table.reload(Menu.tableId);
            table.reload(Menu.tableId);
        }, function (data) {
            Feng.error("核算失败!" + data.responseJSON.message + "!");
            table.reload(Menu.tableId);
        });
        ajax.start();
    });

});